/**
 * 邮件发送相关的API服务
 */

import { request } from '@umijs/max';

// 邮件发送接口定义
export interface EmailSendRequest {
  template_id?: number;
  email_config_id: number;
  recipients: EmailRecipient[];
  subject?: string;
  content?: string;
  template_variables?: Record<string, any>;
  send_immediately?: boolean;
  scheduled_time?: string;
  send_mode?: 'batch' | 'personalized';
  attachments?: any[]; // 附件列表
}

export interface EmailRecipient {
  email: string;
  name?: string;
  variables?: Record<string, any>;
}

export interface EmailSendResponse {
  success: boolean;
  message: string;
  task_id?: string;
  sent_count?: number;
  failed_count?: number;
  details?: any;
}

export interface EmailTask {
  id: string;
  subject: string;
  template_id?: number;
  email_config_id: number;
  recipients_count: number;
  sent_count: number;
  failed_count: number;
  status: EmailTaskStatus;
  send_immediately: boolean;
  scheduled_time?: string;
  created_at: string;
  started_at?: string;
  completed_at?: string;
  error_message?: string;
  user_id: number;
}

export enum EmailTaskStatus {
  PENDING = 'pending',
  RUNNING = 'running',
  COMPLETED = 'completed',
  FAILED = 'failed',
  CANCELLED = 'cancelled',
}

export interface EmailTaskList {
  items: EmailTask[];
  total: number;
  page: number;
  size: number;
  pages: number;
}

export interface EmailSendHistory {
  id: string;
  task_id: string;
  recipient_email: string;
  recipient_name?: string;
  subject: string;
  status: EmailSendStatus;
  sent_at?: string;
  error_message?: string;
  message_id?: string;
}

export enum EmailSendStatus {
  PENDING = 'pending',
  SENT = 'sent',
  FAILED = 'failed',
  BOUNCED = 'bounced',
}

export interface EmailSendHistoryList {
  items: EmailSendHistory[];
  total: number;
  page: number;
  size: number;
  pages: number;
}

export interface EmailStatistics {
  total_tasks: number;
  total_sent: number;
  total_failed: number;
  success_rate: number;
  today_sent: number;
  this_week_sent: number;
  this_month_sent: number;
  recent_tasks: EmailTask[];
}

export interface ContactImportRequest {
  data_source_id: number;
  table_name?: string;
  email_column: string;
  name_column?: string;
  additional_columns?: string[];
  filters?: Record<string, any>;
}

export interface ContactImportResponse {
  success: boolean;
  message: string;
  imported_count: number;
  failed_count: number;
  contacts: EmailRecipient[];
}

// API服务函数
export async function sendEmail(data: EmailSendRequest): Promise<EmailSendResponse> {
  return request('/api/emails/send', {
    method: 'POST',
    data,
  });
}

export async function getEmailTasks(params: {
  page?: number;
  size?: number;
  status?: EmailTaskStatus;
  search?: string;
}): Promise<EmailTaskList> {
  return request('/api/emails/tasks', {
    method: 'GET',
    params,
  });
}

export async function getEmailTask(taskId: string): Promise<EmailTask> {
  return request(`/api/emails/tasks/${taskId}`, {
    method: 'GET',
  });
}

export async function cancelEmailTask(taskId: string): Promise<{ message: string }> {
  return request(`/api/emails/tasks/${taskId}/cancel`, {
    method: 'POST',
  });
}

export async function getEmailSendHistory(params: {
  page?: number;
  size?: number;
  task_id?: string;
  status?: EmailSendStatus;
  search?: string;
}): Promise<EmailSendHistoryList> {
  return request('/api/emails/history', {
    method: 'GET',
    params,
  });
}

export async function getEmailStatistics(): Promise<EmailStatistics> {
  return request('/api/emails/statistics', {
    method: 'GET',
  });
}

export async function importContacts(data: ContactImportRequest): Promise<ContactImportResponse> {
  return request('/api/emails/import-contacts', {
    method: 'POST',
    data,
  });
}

export async function previewEmailContent(data: {
  template_id?: number;
  subject?: string;
  content?: string;
  template_variables?: Record<string, any>;
  recipient?: EmailRecipient;
}): Promise<{
  rendered_subject: string;
  rendered_content: string;
  html_content: string;
}> {
  return request('/api/emails/preview', {
    method: 'POST',
    data,
  });
}
